package com.swjd.linkedlist;

public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        //测试
        System.out.println("双向链表的测试");
        HeroNode2 heroNode1 = new HeroNode2(1, "宋江", "及时雨");
        HeroNode2 heroNode2 = new HeroNode2(2, "卢俊义", "玉麒麟");
        HeroNode2 heroNode3 = new HeroNode2(3, "吴用", "智多星");
        HeroNode2 heroNode4 = new HeroNode2(4, "林冲", "豹子头");

        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.add(heroNode1);
        doubleLinkedList.add(heroNode4);
        doubleLinkedList.add(heroNode3);
        doubleLinkedList.add(heroNode2);
        doubleLinkedList.list();
        HeroNode2 newHero = new HeroNode2(4, "公孙胜", "入云龙");
        doubleLinkedList.update(newHero);
        doubleLinkedList.list();
        doubleLinkedList.delete(3);
        doubleLinkedList.list();
    }
}

//创建一个双向链表的类
class DoubleLinkedList {
    //初始化头节点
    private HeroNode2 head = new HeroNode2(0, "", "");

    //返回头结点
    public HeroNode2 getHead() {
        return head;
    }

    /**
     * 遍历双向链表
     */
    public void list() {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        //定义辅助指针
        HeroNode2 temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            }
            //输出节点信息
            System.out.println(temp);
            temp = temp.next;
        }
    }

    /**
     * 添加节点到最后
     *
     * @param heroNode2 要添加的节点
     */
    public void add(HeroNode2 heroNode2) {
        HeroNode2 temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        //最后一个节点的next指向新节点
        temp.next = heroNode2;
//        新节点的pre指向最后一个节点
        heroNode2.pre = temp;
    }

    /**
     * 修改节点
     *
     * @param heroNode2 修改的数据
     */
    public void update(HeroNode2 heroNode2) {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        //根据no的编号查找到节点
        HeroNode2 temp = head.next;
        while (true) {
            if (temp == null) {
                System.out.printf("没有找到编号为%d的节点，不能修改\n", heroNode2.no);
                break;
            }
            if (temp.no == heroNode2.no) {
                temp.name = heroNode2.name;
                temp.nickname = heroNode2.nickname;
                System.out.println("修改成功");
                break;
            }
            temp = temp.next;
        }
    }

    /**
     * 删除节点
     *
     * @param no 要删除的编号
     */
    public void delete(int no) {
        if (head.next == null) {
            System.out.println("链表为空，无法删除");
            return;
        }
        //辅助指针 temp代表要删除的节点本身
        HeroNode2 temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.no == no) {
                temp.pre.next = temp.next;
                //如果要删除的节点已经是最后一个了，就不需要讲将下一个节点的pre进行指向
                if (temp.next != null) {
                    temp.next.pre = temp.pre;
                }
                System.out.println("删除成功!");
                break;
            }
            temp = temp.next;
        }
    }
}

//定义HeroNode2,每个HeroNode对象就是一个节点 双向链表
class HeroNode2 {
    public int no;//编号
    public String name;//名字
    public String nickname;//昵称
    public HeroNode2 next;//指向下一个节点
    public HeroNode2 pre;//指向前一个节点

    public HeroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode2{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
